package com.xbdun.web.bmp.controller.system;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.xbdun.common.api.XbdUnStringResult;
import com.xbdun.common.constant.XbdunLoginConst;
import com.xbdun.common.exceptions.BizException;
import com.xbdun.comweb.bmp.frame.annotation.XbdunLogin;
import com.xbdun.comweb.bmp.util.UserInfoUtil;
import com.xbdun.facade.bmp.constant.DubboBmpConst;
import com.xbdun.facade.bmp.enumx.SysOrgEnum.SysOrgAuditingStatusEnum;
import com.xbdun.facade.bmp.request.system.SysOrgMenusRequest;
import com.xbdun.facade.bmp.request.system.SysOrgModifyRequest;
import com.xbdun.facade.bmp.request.system.SysOrgPagerRequest;
import com.xbdun.facade.bmp.result.system.SysOrgPagerResult;
import com.xbdun.facade.bmp.result.system.TreeNodeResult;
import com.xbdun.facade.bmp.service.system.SysMenuFacade;
import com.xbdun.facade.bmp.service.system.SysOrgFacade;
import com.xbdun.facade.bmp.service.system.SysOrgMenusFacade;
import com.xbdun.web.bmp.request.PowerOrgMenuRequest;

import lombok.extern.slf4j.Slf4j;

/**
 * @ClassName: S100100103Controller
 * @Description: 企业授权
 * @author wwq
 * @Date 2017年5月11日18:48:56
 */
@Controller
@Slf4j
@XbdunLogin(XbdunLoginConst.XBDADMINT)
public class S100100103Controller extends BaseSystemController{
	@Reference(registry=DubboBmpConst.REGISTRYS)
	SysOrgFacade sysOrgFacade;
	@Reference(registry=DubboBmpConst.REGISTRYS)
	SysMenuFacade sysMenuFacade;
	@Reference(registry=DubboBmpConst.REGISTRYS)
	SysOrgMenusFacade sysOrgMenusFacade;
	
	/**
	 * 企业信息
	 * @return
	 */
	@RequestMapping("100100103")
    public String index(){
        return ROOT_PATH+"100100103";
    }
	
	/**
	 * 检索企业
	 * @param request
	 * @return
	 */
	@RequestMapping("100100103100")
	@ResponseBody
	public SysOrgPagerResult getList(SysOrgPagerRequest request){
		SysOrgPagerResult result = new SysOrgPagerResult();
		try{
			if (request.getAuditingStatus()==0&&request.getEnable()==0&&request.getSelectKey()==0) {
				request.setAuditingStatus(SysOrgAuditingStatusEnum.PASS.getKey());
			}
			log.info("检索企业请求参数:{}", request);
			result = sysOrgFacade.queryPager(request,UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("检索企业失败,入参:{},异常:{}", request, e);;
		}
		return result;
	}
	
	/**
	 * 企业启用停用
	 * @param request
	 * @return
	 */
	@RequestMapping("100100103101")
	@ResponseBody
	public XbdUnStringResult updateEnable(SysOrgModifyRequest request){
		XbdUnStringResult result = null;
		try{
			log.info("企业启用停用,请求参数:{}", request);
			result = sysOrgFacade.modifySysOrg(request, UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result = new XbdUnStringResult();
			log.error("企业启用停用失败,入参:{},异常:{}", request, e);;
		}
		return result;
	}
	
	/**
	 * 企业授权-查询该企业授权菜单树
	 * @return
	 */
	@RequestMapping("100100103102")
	@ResponseBody
	public TreeNodeResult getMenuTree(Long orgCode){
		TreeNodeResult result = new TreeNodeResult();
		try{
			if (orgCode == null||orgCode.longValue()<0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("机构编码不合法");
			}
			result = sysMenuFacade.queryGrantMenu(orgCode,UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("获取当前机构菜单树异常:{}", e);
		}
		return result;
	}
	/**
	 * 给企业授权菜单
	 * @return
	 */
	@RequestMapping("100100103102X")
	@ResponseBody
	public XbdUnStringResult impowerOrgMenus(PowerOrgMenuRequest webRequest){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			log.info("给企业授权菜单请求参数:{}", webRequest);
			if (webRequest.getOrgCode() == null||webRequest.getOrgCode().longValue()<0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("机构编码不合法");
			}
			SysOrgMenusRequest request = new SysOrgMenusRequest();
			request.setOrgCode(webRequest.getOrgCode());
			if (StringUtils.isNotBlank(webRequest.getAddCodesStr())){
				String[] addCodeArr = webRequest.getAddCodesStr().split(",");
				List<Long> addCodes = new ArrayList<Long>();
				for (int i = 0; i < addCodeArr.length; i ++){
					if (StringUtils.isNumeric(addCodeArr[i])) {
						addCodes.add(Long.valueOf(addCodeArr[i]));
					}
				}
				request.setAddCodes(addCodes);
			}
			if (StringUtils.isNotBlank(webRequest.getCancelCodesStr())){
				String[] cancelCodeArr = webRequest.getCancelCodesStr().split(",");
				List<Long> cancelCodes = new ArrayList<Long>();
				for (int i = 0; i < cancelCodeArr.length; i ++){
					if (StringUtils.isNumeric(cancelCodeArr[i])) {
						cancelCodes.add(Long.valueOf(cancelCodeArr[i]));
					}
				}
				request.setCancelCodes(cancelCodes);
			}
			if (CollectionUtils.isNotEmpty(request.getAddCodes()) || CollectionUtils.isNotEmpty(request.getCancelCodes())){
				log.info("给企业授权菜单请求参数:{}", request);
				result = sysOrgMenusFacade.impowerOrgMenus(request,UserInfoUtil.getClient(getRequest()));
			}else {
				throw BizException.PARAM_CHKE_FAIL.newInstance("未进行任何修改!");
			}
			
		}catch(Exception e){
			result.setBizException(e);
			log.error("给企业授权菜单异常:{}", e);
		}
		return result;
	}
	
	/**
	 * 设置代理机构
	 * @param request
	 * @return
	 */
	@RequestMapping("100100103103X")
	@ResponseBody
	public XbdUnStringResult modifyAgentOrg(Long orgCode,int blAgent){
		XbdUnStringResult result = new XbdUnStringResult();
		try{
			if (orgCode == null||orgCode.longValue()<0) {
				throw BizException.PARAM_CHKE_FAIL.newInstance("机构编码不合法");
			}
			log.info("设置代理机构,请求参数:{},{}", orgCode,blAgent);
			result = sysOrgFacade.modifyAgentOrg(orgCode, blAgent,UserInfoUtil.getClient(getRequest()));
		}catch(Exception e){
			result.setBizException(e);
			log.error("设置代理机构失败,入参:{},异常:{}", orgCode, e);;
		}
		return result;
	}
	
}
